package cn.wolfcode.web.controller;

import cn.wolfcode.domain.Department;
import cn.wolfcode.qo.EmployeeQueryObject;
import cn.wolfcode.qo.JSONResult;
import cn.wolfcode.qo.PageResult;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.IDepartmentService;
import cn.wolfcode.util.RequiredPermission;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/department")
public class DepartmentController {

    @Autowired
    private IDepartmentService departmentService;



    //分页查询部门请求
    //@RequiresRoles("admin")
    //允许配置多个权限表达式,默认的判断的逻辑就是与(两个都必须要有)
    //为了方便回去权限的名称,可以把逻辑改成或 or (其中一个有就可能访问)
    @RequiresPermissions(value={"department:list","部门页面"},logical=Logical.OR)
    //@RequiresPermissions("department:list")
    @RequestMapping("/list")
    //@RequiredPermission(name="部门查询",expression = "department:list")
    public String list(Model model, EmployeeQueryObject qo){

        System.out.println("当前用户是否有admin角色:" + SecurityUtils.getSubject().hasRole("admin"));
        System.out.println("当前用登录用户是否有employee:delete权限:" + SecurityUtils.getSubject().isPermitted("employee:delete"));


        PageInfo<Department> pageInfo = departmentService.query(qo);
        model.addAttribute("pageResult", pageInfo);
        model.addAttribute("qo", qo);
        return "department/list";
    }
    //处理删除部门请求
    @RequestMapping("/delete")
    @ResponseBody
    @RequiresPermissions(value ={"department:delete","部门删除"},logical = Logical.OR)
    public JSONResult delete(Long id){


            if (id != null){

                departmentService.delete(id);
            }

            return new JSONResult();
    }

    ////处理新增部门信息
    //@RequestMapping("/input")
    //@RequiredPermission(name="部门新增",expression = "department:input")
    //public String input(Long id,Model model){
    //
    //    if (id != null) {
    //        Department department = departmentService.get(id);
    //        model.addAttribute("department", department);
    //
    //    }
    //
    //    return "department/input";
    //}

    //处理新增和修改页面
    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    @RequiresPermissions(value ={"department:saveOrUpdate","新增和修改"},logical = Logical.OR)
    public JSONResult saveOrUpdate(Department department){

        try {

            if (department.getId() != null) {

                departmentService.update(department);
            }else {
                departmentService.save(department);

            }
            return new JSONResult();
        } catch(Exception e) {
            e.printStackTrace();
        }

        return new JSONResult(false,"操作肆佰");
    }
}
